package ph.com.gs3.formalistics.communicators;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse;
import ph.com.gs3.formalistics.model.valueobjects.application.APIResponse.APIInvalidResponseException;
import ph.com.gs3.formalistics.utilities.FLLogger;

public class DocumentCommunicatorModule extends CommunicatorModule {

	public static String TAG = DocumentCommunicatorModule.class.getSimpleName();

	public DocumentCommunicatorModule(Communicator communicator) {
		super(communicator);

	}

	public DocumentCommunicatorModule(Communicator communicator, String server) {
		super(communicator, server);

	}

	public APIResponse getDocumentStubsToUpdate(String formId, String fromDate)
	        throws CommunicatorException, APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/request-list";

		Map<String, String> parameters = new HashMap<String, String>();

		JSONObject searchParameters = new JSONObject();
		JSONArray fields = new JSONArray();

		fields.put("ID");
		fields.put("DateUpdated");

		try {
			searchParameters.put("fields", fields);

			// add date updated comparison if available
			if (fromDate != null) {
				JSONObject dateUpdatedConditions = new JSONObject();
				dateUpdatedConditions.put("condition", ">=");
				dateUpdatedConditions.put("compared_to_date", fromDate);

				searchParameters.put("date_updated_comparison", dateUpdatedConditions);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		parameters.put("form_id", formId);
		parameters.put("search_parameters", searchParameters.toString());

		// throws CommunicatorException
		String responseString = communicator.getResponseString(url, parameters, "POST");
		JSONObject responseJSON;
		try {
			responseJSON = new JSONObject(responseString);
		} catch (JSONException e) {
			throw new APIInvalidResponseException(e);
		}

		// throws APIResponseException
		return new APIResponse(responseJSON);

	}

	public APIResponse getDocument(String formId, String documentId) throws CommunicatorException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/request";
		Map<String, String> requestParams = new HashMap<String, String>();

		JSONObject searchParameters = new JSONObject();

		try {
			searchParameters.put("include_requestor_info", true);
			searchParameters.put("include_processor_info", true);
		} catch (JSONException e) {
			FLLogger.e(TAG, "Error search preparing parameters, " + e.getMessage());
			e.printStackTrace();
		}

		requestParams.put("form_id", formId);
		requestParams.put("request_id", documentId);

		return getResponse(url, requestParams);
	}

	// public APIResponse getDocuments(List<DocumentStub> stubs, int startingFrom, int
	// documentCount)
	// throws CommunicatorException, APIInvalidResponseException {
	//
	// commonValidation();
	//
	// String url = server + "/API/request-list";
	//
	// Map<String, String> requestParams = new HashMap<String, String>();
	//
	// JSONObject searchParameters = new JSONObject();
	//
	// try {
	// searchParameters.put("include_requestor_info", true);
	// searchParameters.put("include_processor_info", true);
	// } catch (JSONException e) {
	// FLLogger.e(TAG, "Error search preparing parameters, " + e.getMessage());
	// e.printStackTrace();
	// }
	//
	// requestParams.put("form_id", value)
	//
	// return getResponse(url, requestParams);
	// }

	public APIResponse submitDocument(String formId, String requestId, JSONObject requestData,
	        String action) throws CommunicatorException, APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/create-request";

		Map<String, String> requestParams = new HashMap<String, String>();

		requestParams.put("form_id", formId);
		// requestParams.put("request_id", requestId);
		requestParams.put("request_data", requestData.toString());
		requestParams.put("action", action);

		return getResponse(url, requestParams);

	}

}
